﻿XIncludeFile "Function.pb"

;==============================函数类型===============================

;==================================常数==================================
Enumeration
 #Style=1
 #Visible=2
 #Enabled=4
EndEnumeration

;==============================窗口结构==============================
Structure GdipButton Extends PWnd
  Style.B
  MouseStrar.l
EndStructure

;================================函数===========================================

Procedure SetButtonStareImage(*Button.GdipButton,Stare=0)
  With *Button\Creat
  GdipDisposeImage(*Button\Creat\ImageBack)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Center)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Left)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\DownLeft)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Down)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\DownRight)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Right)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\UpRight)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Up)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\UpLeft)   ;绘制缓冲
  Select Stare
    Case 0
      \UpLeft=GdipCatchImage(?ImageButtonUp_UpLeft,?ImageButtonUp_Up-?ImageButtonUp_UpLeft)
      \Up=GdipCatchImage(?ImageButtonUp_Up,?ImageButtonUp_UpRight-?ImageButtonUp_Up)
      \UpRight=GdipCatchImage(?ImageButtonUp_UpRight,?ImageButtonUp_Right-?ImageButtonUp_UpRight)
      \Right=GdipCatchImage(?ImageButtonUp_Right,?ImageButtonUp_DownRight-?ImageButtonUp_Right)
      \DownRight=GdipCatchImage(?ImageButtonUp_DownRight,?ImageButtonUp_Down-?ImageButtonUp_DownRight)
      \Down=GdipCatchImage(?ImageButtonUp_Down,?ImageButtonUp_DownLeft-?ImageButtonUp_Down)
      \DownLeft=GdipCatchImage(?ImageButtonUp_DownLeft,?ImageButtonUp_Left-?ImageButtonUp_DownLeft)
      \Left=GdipCatchImage(?ImageButtonUp_Left,?ImageButtonUp_Center-?ImageButtonUp_Left)
      \Center=GdipCatchImage(?ImageButtonUp_Center,?ImageButtonMove_UpLeft-?ImageButtonUp_Center)
    ;EndCase
     Case 1
      \UpLeft=GdipCatchImage(?ImageButtonMove_UpLeft,?ImageButtonMove_Up-?ImageButtonMove_UpLeft)
      \Up=GdipCatchImage(?ImageButtonMove_Up,?ImageButtonMove_UpRight-?ImageButtonMove_Up)
      \UpRight=GdipCatchImage(?ImageButtonMove_UpRight,?ImageButtonMove_Right-?ImageButtonMove_UpRight)
      \Right=GdipCatchImage(?ImageButtonMove_Right,?ImageButtonMove_DownRight-?ImageButtonMove_Right)
      \DownRight=GdipCatchImage(?ImageButtonMove_DownRight,?ImageButtonMove_Down-?ImageButtonMove_DownRight)
      \Down=GdipCatchImage(?ImageButtonMove_Down,?ImageButtonMove_DownLeft-?ImageButtonMove_Down)
      \DownLeft=GdipCatchImage(?ImageButtonMove_DownLeft,?ImageButtonMove_Left-?ImageButtonMove_DownLeft)
      \Left=GdipCatchImage(?ImageButtonMove_Left,?ImageButtonMove_Center-?ImageButtonMove_Left)
      \Center=GdipCatchImage(?ImageButtonMove_Center,?ImageButtonDown_UpLeft-?ImageButtonMove_Center)
    ;EndCase
     Case 2
      \UpLeft=GdipCatchImage(?ImageButtonDown_UpLeft,?ImageButtonDown_Up-?ImageButtonDown_UpLeft)
      \Up=GdipCatchImage(?ImageButtonDown_Up,?ImageButtonDown_UpRight-?ImageButtonDown_Up)
      \UpRight=GdipCatchImage(?ImageButtonDown_UpRight,?ImageButtonDown_Right-?ImageButtonDown_UpRight)
      \Right=GdipCatchImage(?ImageButtonDown_Right,?ImageButtonDown_DownRight-?ImageButtonDown_Right)
      \DownRight=GdipCatchImage(?ImageButtonDown_DownRight,?ImageButtonDown_Down-?ImageButtonDown_DownRight)
      \Down=GdipCatchImage(?ImageButtonDown_Down,?ImageButtonDown_DownLeft-?ImageButtonDown_Down)
      \DownLeft=GdipCatchImage(?ImageButtonDown_DownLeft,?ImageButtonDown_Left-?ImageButtonDown_DownLeft)
      \Left=GdipCatchImage(?ImageButtonDown_Left,?ImageButtonDown_Center-?ImageButtonDown_Left)
      \Center=GdipCatchImage(?ImageButtonDown_Center,?ImageButtonFlase_UpLeft-?ImageButtonDown_Center)
    ;EndCase
    Case 3
      \UpLeft=GdipCatchImage(?ImageButtonFlase_UpLeft,?ImageButtonFlase_Up-?ImageButtonFlase_UpLeft)
      \Up=GdipCatchImage(?ImageButtonFlase_Up,?ImageButtonFlase_UpRight-?ImageButtonFlase_Up)
      \UpRight=GdipCatchImage(?ImageButtonFlase_UpRight,?ImageButtonFlase_Right-?ImageButtonFlase_UpRight)
      \Right=GdipCatchImage(?ImageButtonFlase_Right,?ImageButtonFlase_DownRight-?ImageButtonFlase_Right)
      \DownRight=GdipCatchImage(?ImageButtonFlase_DownRight,?ImageButtonFlase_Down-?ImageButtonFlase_DownRight)
      \Down=GdipCatchImage(?ImageButtonFlase_Down,?ImageButtonFlase_DownLeft-?ImageButtonFlase_Down)
      \DownLeft=GdipCatchImage(?ImageButtonFlase_DownLeft,?ImageButtonFlase_Left-?ImageButtonFlase_DownLeft)
      \Left=GdipCatchImage(?ImageButtonFlase_Left,?ImageButtonFlase_Center-?ImageButtonFlase_Left)
      \Center=GdipCatchImage(?ImageButtonFlase_Center,?ImageButtonEnd-?ImageButtonFlase_Center)
    ;EndCase
  
  EndSelect 
  EndWith
EndProcedure

Procedure DrawButtonStare(*Button.GdipButton)
  GdipDisposeImage(*Button\Creat\ImageBack) 
  If *Button\Image
     GdipCreateBitmapFromScan0(*Button\Width,*Button\Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@*Button\Creat\ImageBack)
     GdipGetImageGraphicsContext(*Button\Creat\ImageBack,@Graphics)
     GdipDrawImageRectRectI(Graphics, *Button\Image, 0, 0,*Button\Width,*Button\Height, *Button\Width * *Button\Enabled, 0, *Button\Width,*Button\Height, #UnitPixel,#Null, #Null, #Null)
     GdipDeleteGraphics(Graphics)
  Else  
     SetButtonStareImage(*Button,*Button\Enabled)
     GdipCreateBitmapFromScan0(*Button\Width,*Button\Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@*Button\Creat\ImageBack)
     *Button\wSize(*Button,*Button\Width,*Button\Height)
  EndIf 
EndProcedure

Procedure IButText(*Button.GdipButton,Text.s)
  With *Button\Creat
     GdipGetImageWidth(\Left,@X)
     GdipGetImageHeight(\Up,@Y)
     GdipGetImageWidth(\ImageBack,@Width)
     GdipGetImageHeight(\ImageBack,@Height)
     Width-X-GdipImageWidth(\Right)
     Height-Y-GdipImageHeight(\Down)
     
     GdipGetImageGraphicsContext(\ImageBack,@Graphics)
     GdipSetCompositingMode(Graphics,#CompositingModeSourceCopy)
     GdipImageTile(Graphics,\Center,X,Y,Width,Height)
     \Text\Text.s=Text
     TextColor=\Text\TextColor
     PeColor=\Text\PenColor
     Select *Button\Enabled
       Case 0,1
          Y+1
       ;EndCase
       Case 2
         X+1
         Y+2
         Width+1
         Height+1
      ;EndCase
       Case 3
         Y+1
         TextColor=$AA505050
         PenColor= $50505050
       ;EndCase
     EndSelect 
     GdipPathText(Graphics,Text,X,Y,Width,Height,\Text\FontName,\Text\FontSize,\Text\FontStyle,TextColor,PenColor,\Text\PenWidth,\Text\Align,\Text\Style)
     GdipDeleteGraphics(Graphics)
  EndWith
EndProcedure

Procedure IButSize(*Button.GdipButton,Width,Height)
  *Button\Width =Width
  *Button\Height=Height
  With *Button\Creat
    GdipDisposeImage(\ImageBack)
    \ImageBack=GdipCreateImage(Width,Height)
     GdipGetImageGraphicsContext(\ImageBack,@Graphics)
      GdipSetCompositingMode(Graphics,#CompositingModeSourceCopy)
      GdipImageDraw(Graphics,\UpLeft,0,0)
      GdipImageTile(Graphics,\Up,GdipImageWidth(\UpLeft),0,Width-GdipImageWidth(\UpLeft)-GdipImageWidth(\UpRight),GdipImageHeight(\Up))
      GdipImageDraw(Graphics,\UpRight,Width-GdipImageWidth(\UpRight),0)
      GdipImageTile(Graphics,\Right,Width-GdipImageWidth(\Right),GdipImageHeight(\UpRight),GdipImageWidth(\Right),Height-GdipImageHeight(\UpRight)-GdipImageHeight(\DownRight))
      GdipImageDraw(Graphics,\DownRight,Width-GdipImageWidth(\DownRight),Height-GdipImageHeight(\DownRight)) 
      GdipImageTile(Graphics,\Down,GdipImageWidth(\DownLeft),Height-GdipImageHeight(\Down),Width-GdipImageWidth(\DownLeft)-GdipImageWidth(\DownRight),GdipImageHeight(\Down))
      GdipImageDraw(Graphics,\DownLeft,0,Height-GdipImageHeight(\DownLeft))
      GdipImageTile(Graphics,\Left,0,GdipImageHeight(\UpLeft),GdipImageWidth(\Left),Height-GdipImageHeight(\Upleft)-GdipImageHeight(\DownLeft))
    GdipDeleteGraphics(Graphics)
  EndWith
  *Button\wText(*Button,*Button\Creat\Text\Text)
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure

Procedure IButLoad(*Button.GdipButton)
  DrawButtonStare(*Button.GdipButton)
EndProcedure

Procedure IButDraw(*Button.GdipButton,*Gadget.PWnd,*cRect.RECT)

EndProcedure

Procedure IButClick(*Button.GdipButton,hWnd, uMsg, wParam, lParam)
   AddWndEvetn(hWnd,*Button,#PB_EventType_LeftClick)
EndProcedure

Procedure IButEvent(*Button.GdipButton,hWnd, uMsg, wParam, lParam)
  If *Button\Enabled =3
    ProcedureReturn  1
  EndIf 
  Select  uMsg
    Case #WM_LBUTTONDOWN
      *Button\Enabled=2
      DrawButtonStare(*Button)
      SendMessage_(Hwnd,#WM_Draw,*Button,@*Button\X)
      *Button\MouseStrar=uMsg
      *Window.PWnd=*Button\HostGadget
      If *Window
        *Window\OnGadget=*Buttont
      EndIf
   ;EndCase
    ;EndCase
    Case #WM_LBUTTONUP,#WM_MouseToEnte
      If wParam=0
        *Button\Enabled=1
      ElseIf wParam=1
        *Button\Enabled=2
      EndIf
      DrawButtonStare(*Button)
      SendMessage_(Hwnd,#WM_Draw,*Button,@*Button\X)
      If uMsg=#WM_LBUTTONUP  
        If *Button\MouseStrar=#WM_LBUTTONDOWN
           *Button\wClick(*Button,hWnd, uMsg, wParam, lParam)
        Else 
         
        EndIf 
      EndIf 
      *Button\MouseStrar=uMsg
      ;EndCase
    Case #WM_MouseToLeave
      *Button\Enabled=0
      DrawButtonStare(*Button)
      SendMessage_(Hwnd,#WM_Draw,*Button,@*Button\X)
      *Button\MouseStrar=uMsg
     ;EndCase
    Case #WM_LBUTTONDBLCLK
      *Button\Enabled=2
      DrawButtonStare(*Button)
      SendMessage_(Hwnd,#WM_Draw,*Button,@*Button\X)
      AddWndEvetn(hWnd,*Button,#PB_EventType_LeftDoubleClick)
      *Button\MouseStrar=uMsg
     ;EndCase
    Case #WM_MOUSEMOVE

     ;EndCase
  EndSelect
;    Debug *Button\Creat\text\text
  
  ProcedureReturn 1
EndProcedure

Procedure IButIsLoca(*Button.GdipButton,hWnd,X,Y)
  X-*Button\X
  Y-*Button\Y
  If X>-1 And Y>-1 And X<*Button\Width And Y<*Button\Height  And *Button\Visible
     GdipBitmapGetPixel(*Button\Creat\ImageBack,X,Y,@Color)
     If GdipAlpha(Color)
        ProcedureReturn *Button
     EndIf 
  EndIf 
EndProcedure

Procedure IButFree(*Button.GdipButton)
  GdipDisposeImage(*Button\Image)  ;当前绘制的图片
  GdipDisposeImage(*Button\Draw)
  GdipDisposeImage(*Button\Creat\ImageBack)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Center)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Left)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\DownLeft)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Down)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\DownRight)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Right)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\UpRight)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\Up)   ;绘制缓冲
  GdipDisposeImage(*Button\Creat\UpLeft)   ;绘制缓冲
  DeleteWindowList(*Button)
EndProcedure

Procedure GdipCreateButton(*Hwnd.PWnd,Image,X,Y,Width,Height,Text.s="",Style=6,ModeSource=#CompositingModeSourceOver)
 *Button.GdipButton=AddWindowList(@*Hwnd\Gadget,SizeOf(GdipButton))
 If Image
    GdipCloneImage(Image,@*Button\Image)
    GdipGetImageWidth(Image,@Width )
    GdipGetImageHeight(Image,@Height)
    GdipCloneImage(Image,@*Button\Creat\ImageBack)
 Else 
   With *Button\Creat
      SetButtonStareImage(*Button)
      iWidth =GdipImageWidth(\UpLeft)+GdipImageWidth(\UpRight)
      iHeight=GdipImageHeight(\Up)    +GdipImageHeight(\Down)
      If Width <iWidth : Width =iWidth : EndIf
      If Height<iHeight: Height=iHeight: EndIf
      \ImageBack=GdipCreateImage(Width,Height)
    EndWith
 EndIf 
 With *Button\Creat\Text
   \Text=Text
   \FontName="宋体"
   \FontSize=15
   \FontStyle=1
   \TextColor=$FF000000
   \Align= #GD_TextLineAlignCenter | #GD_TextAlignCenter
   \Style=4
 EndWith
 With *Button
    \X=X
    \Y=Y
    If Image
      \Width=Width/4
    Else 
      \Width=Width
    EndIf 
    \Height=Height
    \ModeSource=ModeSource
    \Visible=IsVal(Style,#Visible)
    \Enabled=(1-IsVal(Style,#Enabled))*3
    \Type=#Button
    \Style=IsVal(Style,1)
    \HostGadget=*Hwnd
    *Hwnd\NewGadget=*Button
    \wIsLoca=@IButIsLoca()
    \wLoad=@IButLoad()
    \wDraw=@IButDraw()
    \wEvent=@IButEvent()
    \wFree=@IButFree()
    \wSize=@IButSize()
    \wText=@IButText()   
    \wClick=@IButClick()   
  EndWith
  ProcedureReturn *Button
EndProcedure


DataSection
ImageButtonUp_UpLeft:
  IncludeBinary "Image\Button\Up\UpLeft.png"
ImageButtonUp_Up:
  IncludeBinary "Image\Button\Up\Up.png"
ImageButtonUp_UpRight:
  IncludeBinary "Image\Button\Up\UpRight.png"
ImageButtonUp_Right:
  IncludeBinary "Image\Button\Up\Right.png"
ImageButtonUp_DownRight:
  IncludeBinary "Image\Button\Up\DownRight.png"
ImageButtonUp_Down:
  IncludeBinary "Image\Button\Up\Down.png"
ImageButtonUp_DownLeft:
  IncludeBinary "Image\Button\Up\DownLeft.png"
ImageButtonUp_Left:
  IncludeBinary "Image\Button\Up\Left.png"
ImageButtonUp_Center:
  IncludeBinary "Image\Button\Up\Center.png"

ImageButtonMove_UpLeft:
  IncludeBinary "Image\Button\Move\UpLeft.png"
ImageButtonMove_Up:
  IncludeBinary "Image\Button\Move\Up.png"
ImageButtonMove_UpRight:
  IncludeBinary "Image\Button\Move\UpRight.png"
ImageButtonMove_Right:
  IncludeBinary "Image\Button\Move\Right.png"
ImageButtonMove_DownRight:
  IncludeBinary "Image\Button\Move\DownRight.png"
ImageButtonMove_Down:
  IncludeBinary "Image\Button\Move\Down.png"
ImageButtonMove_DownLeft:
  IncludeBinary "Image\Button\Move\DownLeft.png"
ImageButtonMove_Left:
  IncludeBinary "Image\Button\Move\Left.png"
ImageButtonMove_Center:
  IncludeBinary "Image\Button\Move\Center.png"
  
ImageButtonDown_UpLeft:
  IncludeBinary "Image\Button\Down\UpLeft.png"
ImageButtonDown_Up:
  IncludeBinary "Image\Button\Down\Up.png"
ImageButtonDown_UpRight:
  IncludeBinary "Image\Button\Down\UpRight.png"
ImageButtonDown_Right:
  IncludeBinary "Image\Button\Down\Right.png"
ImageButtonDown_DownRight:
  IncludeBinary "Image\Button\Down\DownRight.png"
ImageButtonDown_Down:
  IncludeBinary "Image\Button\Down\Down.png"
ImageButtonDown_DownLeft:
  IncludeBinary "Image\Button\Down\DownLeft.png"
ImageButtonDown_Left:
  IncludeBinary "Image\Button\Down\Left.png"
ImageButtonDown_Center:
  IncludeBinary "Image\Button\Down\Center.png"
  
ImageButtonFlase_UpLeft:
  IncludeBinary "Image\Button\Flase\UpLeft.png"
ImageButtonFlase_Up:
  IncludeBinary "Image\Button\Flase\Up.png"
ImageButtonFlase_UpRight:
  IncludeBinary "Image\Button\Flase\UpRight.png"
ImageButtonFlase_Right:
  IncludeBinary "Image\Button\Flase\Right.png"
ImageButtonFlase_DownRight:
  IncludeBinary "Image\Button\Flase\DownRight.png"
ImageButtonFlase_Down:
  IncludeBinary "Image\Button\Flase\Down.png"
ImageButtonFlase_DownLeft:
  IncludeBinary "Image\Button\Flase\DownLeft.png"
ImageButtonFlase_Left:
  IncludeBinary "Image\Button\Flase\Left.png"
ImageButtonFlase_Center:
  IncludeBinary "Image\Button\Flase\Center.png"

ImageButtonEnd:
EndDataSection





; IDE Options = PureBasic 4.30 (Windows - x86)
; CursorPosition = 105
; Folding = Iz+--
; EnableAsm
; EnableUnicode
; EnableXP
; HideErrorLog
; CompileSourceDirectory